﻿using System;
using System.Linq;
using System.Windows.Data;
using MVVM_S.Samples.HelpDesk.ViewModels;
using MVVM_S.Samples.HelpDesk.Web.Models;

namespace MVVM_S.Samples.HelpDesk.Converters
{
    public class OfficeConverter : IValueConverter
    {
        PagedCollectionView lookupCollection;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (lookupCollection == null)
            {
                LookupsViewModel lookupModel = (LookupsViewModel)App.Current.Resources["LookupsVM"];
                lookupCollection = lookupModel.OfficeList;
            }
            if (value is string)
            {
                return lookupCollection.Cast<Office>()
                    .Single(o => o.OfficeID.Equals((string)value)).OfficeName;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
